<?php
// vim:set expandtab tabstop=3 shiftwidth=3 softtabstop=3 foldclose=all foldmethod=marker:
/**
 * 项目：   5anet
 * 文件：   class/album/AlbumUtil.class.php
 *
 * 相册工具类
 *
 * PHP Version 5
 *
 * @package:   class.album
 * @author:    Mike.G Chinese Name: 黄叶 <hylinux@gmail.com>
 * @license:   http://www.gnu.org/copyleft/lesser.html  LGPL License 2.1 
 * @copyright: www.5anet.com
 * @version:   $Id: $
 * @date:      $Date: $
 */

include_once FUNCTION_PATH.'utf8_substr.fun.php';
include_once FUNCTION_PATH.'ConvertString.fun.php';


class AlbumUtil {
   
   /**
    * 得到相册的分类列表
    * @param:  &$db 数据库连接类
    * @return: $category_info
    * @access: public
    * @static
    */
   public static function &getAllCategory(&$db) {/*{{{*/
      $sql = 'select id, name from album_type ';
      $res = $db->Execute($sql);

      $category = array();

      while ( $rows = $res->FetchRow() ) {
         $category[] = array(
            'id'=>$rows['id'],
            'name'=>$rows['name'],
            );
      }
      return $category;
   }/*}}}*/

   /**
    * 得到相册的相片浏览信息
    * @param:  &$db, 数据库的连接信息
    * @param:  $id   相片的分类id
    * @param:  $limit 数量
    * @param:  $offset  偏移数量
    * @return: $array_info 
    * @access: public
    * @static
    */
   public static function &getAlbumInfo(&$db, $id, $limit=20, $offset_page=0) {/*{{{*/

      $sql = 'select id, user_id, dir_id, name, size, image_type, clicks, rates from '.
         ' album ';

      if ( $id ) {
         $sql .= ' where type=?';
      }

      $sql .= ' order by id desc ';

      $res = null;

      if ( $id ) {
         $res = $db->CacheSelectLimit(120, $sql, $limit, $offset_page, array($id));
      } else {
         $res = $db->CacheSelectLimit(120, $sql, $limit, $offset_page);
      }

      $array_info = array();

      while($rows = $res->FetchRow() ) {
         $array_info[] = array(
            'id'=>$rows['id'],
            'dir_id'=>$rows['dir_id'],
            'name'=>$rows['name'],
            'sname'=>utf8_substr($rows['name'], 0, 6),
            'size'=>$rows['size'],
            'type'=>$rows['image_type'],
            'img'=>ROOT_URL.'upload/album/'.$rows['user_id'].'/'.$rows['id'].'-117-83.'.$rows['image_type'],
            );
      }

      return $array_info;

   }
/*}}}*/

   /*
    * 判断相片的分类id是否存在
    * @param:  &$db, 数据库连接
    * @param:  $id   相片的分类
    * @return: $isExists 
    * @access: public
    * @static
    */
   public static function isExists(&$db, $id) {/*{{{*/
      $sql = 'select count(*) as num from album_type where id=?';
      $sth = $db->prepare($sql);
      $res = $db->Execute($sth, array($id));
      $rows = $res->FetchRow();

      return $rows['num'];

   }/*}}}*/

   /**
    * 判断相片是否存在
    * @param:  &$db  数据库连接
    * @param:  $id   相片的id
    * @return $isExists
    * @access: public
    * @static
    */
   public static function isAlbumExists(&$db, $id) {/*{{{*/
      $sql = 'select count(*) as num from album where id=?';
      $sth = $db->prepare($sql);
      $res = $db->Execute($sth, array($id));
      $rows = $res->FetchRow();

      return $rows['num'];
   }/*}}}*/


   /**
    * 查看相片的id
    * @param:  &$db  数据库连接
    * @param:  $id   相片的id
    * @return: $album_info
    * @access: public
    * @static
    */
   public static function &getPhotoInfo(&$db, $id) {/*{{{*/
      $sql = 'select id, dir_id, name, size, image_type, clicks, '.
         'rates, user_id, description from album where id=?';
      $sth = $db->prepare($sql);
      $res = $db->Execute($sth, array($id));
      $rows = $res->FetchRow();


      $photo_info = array (
         'id'=>$rows['id'],
         'name'=>$rows['name'],
         'size'=>$rows['size'],
         'clicks'=>$rows['clicks'],
         'rates'=>$rows['rates'],
         'image_path'=>ROOT_URL.'upload/album/'.$rows['user_id'].'/'.
         $rows['id'].'-460-390.'.$rows['image_type'],
         'n_path'=>ROOT_URL.'upload/album/'.$rows['user_id'].'/'.
         $rows['id'].'.'.$rows['image_type'],
         'description'=>$rows['description'],
         'user_id'=>$rows['user_id'],
         'dir_id'=>$rows['dir_id'],

      );


      return $photo_info;

   }/*}}}*/
   
   /**
    * 取得相片目录的名字
    * @param:  &$db, 数据库连接
    * @param:  $id   相片目录的id
    * @return: $dir_name   目录的名字
    * @access: public
    * @static
    */
   public static function getDirName(&$db, $id) {/*{{{*/
      $sql = 'select name from album_dir where id=?';
      $sth = $db->prepare($sql);
      $res = $db->Execute($sth, array($id));
      $rows = $res->FetchRow();

      return $rows['name'];

   }/*}}}*/

   
   /**
    * 取得相片的评论
    * @param:  &$db  数据库连接
    * @param:  $id   相片的id
    * @return  $comment_info  相片的评论
    * @access: public
    * @static
    */
   public static function &getPhotoComment(&$db, $id) {/*{{{*/
      $sql = 'select user, submitDate, fromaddr, content from album_comment where image_id=?'.
         ' order by id asc';
      $sth = $db->prepare($sql);
      $res = $db->Execute($sth, array($id));
      
      $comment = array();

      while ( $rows = $res->FetchRow() ) {
         $comment[] = array (
            'author'=>$rows['user'],
            'submittime'=>$rows['submitDate'],
            'fromaddr'=>$rows['fromaddr'],
            'content'=>ConvertString($rows['content'],  ROOT_URL, IMAGE_URL.'express/')
         );
      }

      return $comment;
   }/*}}}*/

   /**
    * 取得用户相册
    * @param:  &$db  数据库连接
    * @param:  $id   用户的id
    * @return: $dir  相册数组
    * @access: public
    * @static
    */
   public static function &getUserDir(&$db, $id) {/*{{{*/
      $sql = 'select id, name from album_dir where user_id=? order by id desc';
      $sth = $db->prepare($sql);
      $res = $db->Execute($sth, array($id));
      $rows = $res->GetRows();
      
      return $rows;

   }/*}}}*/

   /**
    * 判断用户查看的目录是否存在
    * @param:  &$db  数据库连接
    * @param:  $dir  目录的id
    * return:  $isExists   
    * @access: public
    * @static
    */
   public static function isDirExists(&$db, $id) {/*{{{*/
      $sql = 'select count(*) as num from album_dir where id=?';
      $sth = $db->prepare($sql);
      $res = $db->Execute($sth, array($id));
      $rows = $res->FetchRow();

      return $rows['num'];
   }/*}}}*/

   /**
    * 判断用户查看的目录是否存在,
    * 根据名称
    * @param:  &$db  数据库连接
    * @param:  $dir  目录的id
    * return:  $isExists   
    * @access: public
    * @static
    */
   public static function DirhadExists(&$db, $name) {/*{{{*/
      $sql = 'select count(*) as num from album_dir where name=?';
      $sth = $db->prepare($sql);
      $res = $db->Execute($sth, array($name));
      $rows = $res->FetchRow();

      return $rows['num'];
   }/*}}}*/



   /**
    * 根据需要查看的相册的目录，取得用户的id
    * @param:  &$db  数据库连接
    * @param:  $id   目录的id
    * @return: $user_id 用户的id
    * @access: public
    * @static
    */
   public static function getUserIdByDir(&$db, $id) {/*{{{*/
      $sql = 'select user_id from album_dir where id=?';
      $sth = $db->prepare($sql);
      $res = $db->Execute($sth, array($id));
      $rows = $res->FetchRow();

      return $rows['user_id'];
   }/*}}}*/

   
   /**
    * 根据相册的目录求的该目录里有的相片数量
    * @param:  &$db  数据库连接
    * @param:  $dir  目录的ID
    * @param:  $pre_page   每页显示相片的数量
    * @return: $total_page 总数量
    * @access: public
    * @static
    */
   public static function getTotalPageByDir(&$db, $dir, $pre_page) {/*{{{*/
      $sql = 'select count(*) as num from album where dir_id=?';
      $sth = $db->prepare($sql);
      $res = $db->Execute($sth, array($dir));
      $rows = $res->FetchRow();
      
      if ( $pre_page == 0 ) {
         $pre_page = 40;
      }

      return ceil( $rows['num'] / $pre_page );

   }/*}}}*/

   /**
    * 取得特定目录下的相片的信息。
    * @param:  &$db  数据库连接
    * @param:  $dir  相册目录
    * @param:  $pre_page   需要显示的数量
    * @param:  $offset_page   偏移的数据量
    * @return: $photo_info 相片的信息
    * @access: public
    * @static
    */
   public static function &getPhotoFromDirByPage(&$db, $dir, $pre_page, $offset_page ) {/*{{{*/
      $sql = 'select id, dir_id, name, size, image_type, clicks, rates, user_id '.
         ' from album where dir_id=? order by id desc ';
      $res = $db->SelectLimit($sql, $pre_page, $offset_page, array($dir));

      $photo_info = array();

      while ( $rows = $res->FetchRow() ) {
         $photo_info[] = array (
            'id'=>$rows['id'],
            'dir_id'=>$rows['dir_id'],
            'name'=>$rows['name'],
            'sname'=>utf8_substr($rows['name'], 0, 6),
            'size'=>$rows['size'],
            'img'=>ROOT_URL.'upload/album/'.$rows['user_id'].'/'.$rows['id'].'-117-83.'.
            $rows['image_type'],
          );


      }

      return $photo_info;



   }/*}}}*/

   /**
    * 删除目录下的所有相片
    * @param:  &$db  数据库连接
    * @param:  $id   目录id
    * @return: NULL
    * @access: public
    * @static
    */
   public static function DelPhotoByDir(&$db, $id, $user_id) {/*{{{*/
      $sql = 'select id, image_type from album where dir_id=?';
      $sth = $db->prepare($sql);
      $res = $db->Execute($sth, array($id));

      while ( $rows = $res->FetchRow() ) {
         $image_file = ROOT_PATH.'upload/album/'.$user_id.'/'.$rows['id'];

         if ( file_exists($image_file.'.'.$rows['image_type']) ) {
            unlink($image_file.'.'.$rows['image_type']);
         }

         if ( file_exists($image_file.'-460-390.'.$rows['image_type']) ) {
            unlink($image_file.'-460-390.'.$rows['image_type']);
         }

         if ( file_exists($image_file.'-117-83.'.$rows['image_type']) ) {
            unlink($image_file.'-117-83.'.$rows['image_type']);
         }
      }

      $sql = 'delete from album where dir_id=?';
      $sth = $db->prepare($sql);
      $db->Execute($sth, array($id));

      return;

   }/*}}}*/


   /**
    * 删除相册
    * @param:  &$db  数据库连接
    * @param:  $id   目录的id
    * @return: NULL
    * @access: public
    * @static
    */
   public static function DelDir(&$db, $id) {/*{{{*/
      $sql = 'delete from album_dir where id=?';
      $sth = $db->prepare($sql);
      $db->Execute($sth, array($id));
   }/*}}}*/
   

   /**
    * 判断相册目录是否是属于用户
    * @param:  &$db  数据库连接
    * @param:  $id   目录的id
    * @param:  $user_id 用户的id
    * @return: $isBelongto 
    * @access: public
    * @static
    */
   public static function dirBelongToUser(&$db, $id, $user_id) {/*{{{*/
      $sql = 'select count(*) as num from album_dir where id=? and user_id=?';
      $sth = $db->prepare($sql);
      $res = $db->Execute($sth, array($id, $user_id));
      $rows = $res->FetchRow();
      
      return $rows['num'];

   }/*}}}*/

   /**
    * 判断相片是否是属于用户
    * @param:  &$db  数据库连接
    * @param:  $id   相片的id
    * @param:  $user_id 用户的id
    * @return  $isBelongto
    * @access  public
    * @static
    */
   public static function photoBelongToUser(&$db, $id, $user_id ) {/*{{{*/
      $sql = 'select count(*) as num from album where id=? and user_id=?';
      $sth = $db->prepare($sql);
      $res = $db->Execute($sth, array($id, $user_id));
      $rows = $res->FetchRow();
      
      return $rows['num'];

   }/*}}}*/


   /**
    * 根据相片的id求得相册的id
    * @param:  &$db  数据库连接
    * @param:  $id   相片的id
    * @return: $dir_id
    * @access: public
    * @static
    */
   public static function getDirIdByPhoto(&$db, $id) {/*{{{*/
      $sql = 'select dir_id from album where id=?';
      $sth = $db->prepare($sql);
      $res = $db->Execute($sth, array($id));
      $rows = $res->FetchRow();

      return $rows['dir_id'];
   }/*}}}*/

   /**
    * 删除相片
    * @param:  &$db  数据库连接
    * @param:  $id   相片的id
    * @return: nULL
    * @access: public
    * @static
    */
   public static function DelPhotoById(&$db, $user_id, $id) {/*{{{*/
      $sql = 'select id, image_type from album where id=?';
      $sth = $db->prepare($sql);
      $res = $db->Execute($sth, array($id));

      $rows = $res->FetchRow();
      $image_file = ROOT_PATH.'upload/album/'.$user_id.'/'.$rows['id'];

      if ( file_exists($image_file.'.'.$rows['image_type']) ) {
         echo "1"."<br>";
           unlink($image_file.'.'.$rows['image_type']);
      }

      if ( file_exists($image_file.'-460-390.'.$rows['image_type']) ) {
            unlink($image_file.'-460-390.'.$rows['image_type']);
      }

      if ( file_exists($image_file.'-117-83.'.$rows['image_type']) ) {
            unlink($image_file.'-117-83.'.$rows['image_type']);
      }
      

      $sql = 'delete from album where id=?';
      $sth = $db->prepare($sql);
      $db->Execute($sth, array($id));
      return;
   }
/*}}}*/

   /**
    * 根据给出的类型求得所有的页面
    * @param:  &$db  数据库连接
    * @param:  $type  相片的类型
    * @param:  $pre_page   每一页显示的照片数
    * @return: $total_page 共有的相片数
    * @access: public
    * @static
    */
   public static function getTotalPageByType(&$db, $type = 0 , $pre_page = 24) {
      $sql = 'select count(*) as num from album ';

      if ( $type ) {
         $sql .= " where type='$type' ";
      }

      $res = $db->Execute($sql);
      $rows = $res->FetchRow();
      
      if ( $pre_page == 0 ) {
         $pre_page = 40;
      }

      return ceil( $rows['num'] / $pre_page );

   }



}



?>
